/*
Source : https://leetcode.com/problems/single-number-ii/
Author : nflush@outlook.com
Date   : 2016-07-14
*/


/*
137. Single Number II
 ?  

Question Editorial Solution  
 My Submissions 




?Total Accepted: 89363
?Total Submissions: 232315
?Difficulty: Medium



Given an array of integers, every element appears three times except for one. Find that single one. 

Note:
 Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 



Subscribe to see which companies asked this question
*/
class Solution {
public:
    int singleNumber(vector<int>& nums) {
        int one = 0;
        int two = 0;
        for (int n:nums){
            int x = n & ~one;
            int y = n & one;
            int z = y & two;
            one ^= x | z;
            two ^= y ;
        }
        return one;
    }
};
